-- CostConfirmDialog
-- 消耗确认Dialog

local CostConfirmDialog = class("CostConfirmDialog",function()
    return display.newModalDialog()
end)

-- params = {
--     str_1 = ,
--     str_2 = ,
--     str_3 = ,
--     costNum = ,
--     costIconPath = ,
--     onConfirm = ,
--     onCancel =
-- }

local initPosList = { 0,0,0 }
function CostConfirmDialog:ctor( params )
    local str_1 = params.str_1 or ""
    local str_2 = params.str_2 or ""
    local str_3 = params.str_3 or ""
    local costNum = params.costNum or 0
    local costIconPath = params.costIconPath or ""
    local onConfirmCB = params.onConfirm
    local onCancelCB = params.onCancel

    local rootNode = cc.uiloader:load(Res.CostConfirmDialogUI)
    if not rootNode then
        return
    end

    self.mUINode = rootNode
    self:addChild(rootNode)

    local timelineAction = rootNode:getActionByTag(0)
    if timelineAction then
        timelineAction:play("fadein",false)
    end

    rootNode:setContentSize(cc.size(display.width,display.height))
    ccui.Helper:doLayout(rootNode)

    local tipsNode = rootNode:getChildByName( "tips" )

    self._str_1 = tipsNode:getChildByName("text_0_0")
    self._str_1:setString( str_1 )

    local Panel_num = tipsNode:getChildByName( "Panel_num" )
    self._str_2 = Panel_num:getChildByName( "text" )
    self._str_2:setString( str_2 )

    self._costNum = Panel_num:getChildByName("num")
    self._costNum:setString( costNum )

    self._costIcon = Panel_num:getChildByName("icon")
    self._costIcon:loadTexture( costIconPath )

    display.setNodesMiddle( Panel_num, nil, self._str_2, self._costNum, self._costIcon )

    self._str_3 = tipsNode:getChildByName("text_0")
    self._str_3:setString( str_3 )

    for i = 1, 3 do
        initPosList[i] = self["_str_"..i]:getPositionY()
    end
    self:adjustPos()

    local confirmButton = tipsNode:getChildByName("Button_queding")
    self._confirmBtnName = confirmButton:getChildByName( "name" )
    confirmButton:addClickEventListener(function(sender)
         if onConfirmCB then
            onConfirmCB( self )
        end
    end)

    local cancelButton = tipsNode:getChildByName("Button_quxiao")
    self._cancelBtnName = confirmButton:getChildByName( "name" )
    cancelButton:addClickEventListener(function(sender)
        if onCancelCB then
            onCancelCB(self)
        end
    end)
end

function CostConfirmDialog:setStr_2(hint)
    self._str_2:setString( hint or "" )
end

function CostConfirmDialog:setCostNum( num )
    self._costNum:setString( num or  "" )
end

function CostConfirmDialog:setCostIcon( iconPath )
    if iconPath and "" ~= iconPath then
        self._costIcon:loadTexture( iconPath )
    end
end

function CostConfirmDialog:setStr_3( text )
    self._str_3:setString( text or "" )
end

function CostConfirmDialog:setConfirmText( text )
    self._confirmBtnName:setString( text or "" )
end

function CostConfirmDialog:setCancelText( text )
    self._cancelBtnName:setString( text or "" )
end

function CostConfirmDialog:adjustPos()
    local str_1 = self._str_1:getString()
    if "" == str_1 then
        for i = 2, 3 do
            self["_str_"..i]:setPositionY( initPosList[i] + 20 )
        end
    else
        for i = 1, 3 do
            self["_str_"..i]:setPositionY( initPosList[i] )
        end
    end
    self._costNum:setPositionY( self._str_2:getPositionY() )
    self._costIcon:setPositionY( self._str_2:getPositionY() )
end

return CostConfirmDialog
